package com.imay.base.util;

import android.content.Context;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

/**
 * Created by jeaman on 2017/4/20.
 */

public class FileUtil {

    /**
     * 描述：从文件读取byte[].
     *
     * @param file 文件路径
     * @return byte[]
     */
    public static byte[] getByteArray(File file) {
        byte[] bytes = null;
        ByteArrayOutputStream out = null;
        try {
            // 文件是否存在
            if (!file.exists()) {
                return null;
            }

            long fileSize = file.length();
            if (fileSize > Integer.MAX_VALUE) {
                return null;
            }

            FileInputStream in = new FileInputStream(file);
            out = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            int size = 0;
            while ((size = in.read(buffer)) != -1) {
                out.write(buffer, 0, size);
            }
            in.close();
            bytes = out.toByteArray();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                }
            }
        }
        return bytes;
    }

    /**
     * 描述：将byte数组写入文件.
     *
     * @param file
     * @param content
     */
    public static void writeByteArray(File file, byte[] content) {
        FileOutputStream fos = null;
        try {
            // 文件是否存在
            if (!file.exists()) {
                File parent = file.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                    file.createNewFile();
                }
            }
            fos = new FileOutputStream(file);
            fos.write(content);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                }
            }
        }
    }

    public static String getString(File file) {
        byte[] bytes = getByteArray(file);
        if (bytes != null) {
            return new String(bytes);
        }
        return null;
    }

    public static void writeString(File file, String content) {
        if (content != null) {
            writeByteArray(file, content.getBytes());
        }
    }

    /**
     * 描述：读取Assets目录的文件内容.
     *
     * @param context
     * @param name
     * @return the string
     */
    public static String readAssetsByName(Context context, String name) {
        String text = null;
        InputStreamReader inputReader = null;
        BufferedReader bufReader = null;
        try {
            inputReader = new InputStreamReader(context.getAssets().open(name));
            bufReader = new BufferedReader(inputReader);
            String line = null;
            StringBuffer buffer = new StringBuffer();
            while ((line = bufReader.readLine()) != null) {
                buffer.append(line);
            }
            text = new String(buffer.toString().getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufReader != null) {
                    bufReader.close();
                }
                if (inputReader != null) {
                    inputReader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return text;
    }

    /**
     * 移动文件
     * @param srcFileName    源文件完整路径
     * @param destDirName    目的目录完整路径
     * @return 文件移动成功返回true，否则返回false
     */
    public static boolean moveFile(String srcFileName, String destDirName) {
        File srcFile = new File(srcFileName);
        if (!srcFile.exists() || !srcFile.isFile()) {
            return false;
        }

        File destDir = new File(destDirName);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }

        return srcFile.renameTo(new File(destDirName + File.separator + srcFile.getName()));
    }

    //删除文件
    public static void deleteFile(String srcFileName) {
        if (srcFileName == null) {
            return;
        }
        File file = new File(srcFileName);
        if (file.exists()) {
            file.delete();
        }
    }
}
